from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
import datetime

# 自定义字段 tinyint
class CustomeTinyIntField(models.Field):
    def __init__(self, max_length, *args, **kwargs):
        self.max_length = max_length
        super(CustomeTinyIntField, self).__init__(max_length=max_length, *args, **kwargs)

    def db_type(self, connection):
        return 'tinyint(%s)' % self.max_length



# 栏目模型
class Category(models.Model):
    cate_name = models.CharField(max_length=25, default='', verbose_name='栏目名称')
    describles = models.CharField(max_length=200, default='', verbose_name='栏目描述')
    # ImageField字段可通过传参的方式upload_to规定图片的上传地址
    picture = models.ImageField(max_length=150, upload_to='static/reception/%Y%m%d/', default='', verbose_name='栏目图片')
    is_menu = models.BooleanField(default=True, verbose_name='是否在首页栏目中展示')
    weight = models.PositiveSmallIntegerField(default=0, verbose_name='控制栏目排序')
    seo_title = models.CharField(max_length=50, verbose_name='seo标题')
    seo_keyword = models.CharField(max_length=60, verbose_name='seo关键词')
    seo_description = models.CharField(max_length=200, verbose_name='seo描述')
    count = models.PositiveIntegerField(default=0, verbose_name='发布文章数')
    # 使用双下方法，改变页面的提示信息（默认是一个对象）
    def __str__(self):
        return self.cate_name
    class Meta:
        verbose_name= '栏目管理'
        verbose_name_plural = '栏目管理'


# 文章模型
class Article(models.Model):
    title = models.CharField(max_length=150, verbose_name='标题')
    category = models.ForeignKey(to="Category", on_delete=models.SET_NULL,null=True, verbose_name='栏目id')
    # 文章中的关键词
    tags = models.CharField(max_length=50, default='', verbose_name='标签')
    describles = models.CharField(max_length=200, verbose_name='描述')
    picture = models.CharField(max_length=200, default='', verbose_name='图片')
    # 文章推荐标识
    flag = CustomeTinyIntField(max_length=1, default=0, verbose_name='文章推荐 0默认 1置顶 2 推荐')
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    # 发布人
    manager = models.ForeignKey(to=User, on_delete=models.SET_NULL, null=True,verbose_name='添加人')
    # 点击量，文章每次被打开都会增加1
    click = models.PositiveIntegerField(verbose_name='点击量', default=0)
    # 点赞量，文章被点赞的数量
    support = models.PositiveIntegerField(verbose_name='点赞', default=0)
    seo_title = models.CharField(max_length=50, verbose_name='seo标题')
    seo_keyword = models.CharField(max_length=60, verbose_name='seo关键词')
    seo_description = models.CharField(max_length=200, verbose_name='seo描述')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = '栏目管理'


# 文章附加表（内容表）
class Atricle_additional(models.Model):
    article = models.OneToOneField(to='Article', primary_key=True, default=0, on_delete=models.CASCADE)
    content = models.TextField(verbose_name='文章的内容')